package com.pox.guli.service.edu.controller.admin;


import com.pox.guli.common.base.result.R;
import com.pox.guli.common.base.result.ResultCodeEnum;
import com.pox.guli.service.base.exception.PoXException;
import com.pox.guli.service.edu.entity.vo.SubjectVo;
import com.pox.guli.service.edu.service.SubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author pox21s
 * @since 2022-06-01
 */

@Slf4j
//@CrossOrigin
@Api(description = "学科管理")
@RestController
@RequestMapping("/admin/edu/subject")
public class SubjectController {

    @Resource
    private SubjectService subjectService;

    @ApiOperation("课程列表Excel导入")
    @PostMapping("/import")
    public R batchImportByExcel(@ApiParam(value = "上传的文件", required = true)
                                @RequestParam("file") MultipartFile file) {

        try {
            InputStream inputStream = file.getInputStream();
            subjectService.batchImport(inputStream);

            return R.ok().message("文件批量导入成功。");
        } catch (Exception e) {
            log.info("Excel文件导入错误，信息为：" + e.getMessage());
            throw new PoXException(ResultCodeEnum.EXCEL_DATA_IMPORT_ERROR);
        }

    }

    @ApiOperation(value = "嵌套数据列表")
    @GetMapping("/nested-list")
    public R nestedList() {

        List<SubjectVo> subjectVoList = subjectService.nestedList();

        return R.ok().data("items", subjectVoList);
    }

}

